package com.crazy.appium.testng;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import org.testng.log4testng.Logger;

import com.crazy.appium.driver.CrazyPath;
import com.crazy.appium.page.HomePage;
import com.crazy.appium.testcases.TestBase;

public class TestngListener extends TestListenerAdapter {
	private Logger logger = Logger.getLogger(TestngListener.class);

	@Override
	public void onTestFailure(ITestResult tr) {
		super.onTestFailure(tr);
		logger.info(tr.getName() + " Failure");
		// takeScreenShot(tr);
		TestBase tb = (TestBase) tr.getInstance();
		try {
			Long date = System.currentTimeMillis();
			System.out.println(date);
			Reporter.log("<a href=http://localhost:8080/" + tr.getName() + tb.getUdid().split(":")[0]
					+ Thread.currentThread().getId() + "_" + date + ".png" + " target=_blank>失败截图</a>", true);
			Reporter.log("<img src=http://localhost:8080/" + tr.getName() + tb.getUdid().split(":")[0]
					+ Thread.currentThread().getId() + "_" + date + ".png" + " style=width:30px;height:30px img/>",
					true);
			tb.getDriver().takeScreen("D:/apache-tomcat-8.5.4-windows-x64/apache-tomcat-8.5.4/webapps/ROOT/",
					tr.getName() + tb.getUdid().split(":")[0] + Thread.currentThread().getId() + "_" + date);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		HomePage homepage=new HomePage(tb.getDriver(),tb.getPlatform());
//		while(!tb.getDriver().getPageSource().contains("我的")){
//			tb.getDriver().findElement(By.id("com.hotyq.app.android:id/title_back")).click();
//		}
	}

	@Override
	public void onTestSkipped(ITestResult tr) {
		super.onTestSkipped(tr);
		logger.info(tr.getName() + " Skipped");
		// takeScreenShot(tr);
	}

	@Override
	public void onTestSuccess(ITestResult tr) {
		super.onTestSuccess(tr);
		logger.info(tr.getName() + " Success");
	}

	@Override
	public void onTestStart(ITestResult tr) {
		super.onTestStart(tr);
		logger.info(tr.getName() + " Start");
	}

	@Override
	public void onFinish(ITestContext testContext) {

	}
}